#!/usr/bin/env ruby

# change to current directory when invoked on its own
Dir.chdir(File.dirname(File.expand_path(__FILE__))) if __FILE__ == $0

# add current directory to load path
$FLUXR_HOME = File.dirname(File.expand_path(__FILE__))
$: << $FLUXR_HOME

require 'rubygems'
gem 'camping', '~> 1.5'
require 'camping'

require 'active_support'
require 'yaml'

Camping.goes :Yarbo

module Yarbo
  def init_logger
    $LOG = Yarbo::Utils::Logger.new(Yarbo::Conf.log[:file])
    $LOG.level = "Yarbo::Utils::Logger::#{Yarbo::Conf.log[:level]}".constantize
  end
  module_function :init_logger

  def init_db_logger
    begin
      if Yarbo::Conf.db_log
        log_file = Yarbo::Conf.db_log[:file] || 'yarbo_db.log'
        Yarbo::Models::Base.logger = Logger.new(log_file)
        Yarbo::Models::Base.logger.level = "Yarbo::Utils::Logger::#{Yarbo::Conf.db_log[:level] || 'DEBUG'}".constantize
      end
    rescue Errno::EACCES => e
      $LOG.warn "Can't write to database log file at '#{log_file}': #{e}"
    end
  end
  module_function :init_db_logger

end

require 'yarbo/utils'
require 'yarbo/models'
require 'yarbo/conf'
require 'yarbo/views'
require 'yarbo/controllers'